package com.poetry.vup.common.exception;

import com.poetry.vup.common.result.BaseResult;
import com.poetry.vup.common.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一的异常处理
 * @author mileswan
 *
 */
@ControllerAdvice
@Slf4j
public class KabobExceptionHandler {
 
    /**
      * @Author: mwan
      * @Description: 系统异常捕获处理
      */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public BaseResult<?> javaExceptionHandler(Exception ex) {//APIResponse是项目中对外统一的出口封装，可以根据自身项目的需求做相应更改
    	//异常写入普通日志
    	log.error("捕获到Exception异常",ex);
    	
        return BaseResult.failed(ResultCode.FAILED, (ex.getMessage() == null || ex.getMessage().replace(" ", "").equals("") )?"服务器发生异常":
        	ex.getMessage());
    }
 
    /**
      * @Author: mwan
      * @Description: 自定义异常捕获处理
      */
    @ResponseBody
    @ExceptionHandler(value = KabobException.class)//KabobException是自定义的一个异常
    public BaseResult<?> kabobExceptionHandler(KabobException ex) {
    	//异常写入普通日志
    	log.error("捕获到KabobException异常",ex.getException());
    	
        return ex.getApiResponse();
    }
    
    /**
     * @Author: mwan
     * @Description: 自定义警报捕获处理
     */
   @ResponseBody
   @ExceptionHandler(value = KabobWarning.class)//KabobWarning是自定义的一个警报
   public BaseResult<?> sstarWarningHandler(KabobWarning ex) {
   	
       return ex.getApiResponse();
   }
 
}

